<?php

class HtmlUtil {

    function dateFormat($format, $date)
    {
    	if ($date == 0 || $date == "" || $date == null)
    		return "";
    	return date($format, strtotime($date));
    }

    function formatTimeOnly($date)
    {
    	// TODO format the date!
    	return date("g:i a", strtotime($date));
    }
    function formatDateOnly($date)
    {
    	// TODO format the date!
    	return date("D, F j, Y", strtotime($date));
    }
    function formatDate($date)
    {
    	// TODO format the date!
    	return date("D, F j, Y  h:i a", strtotime($date));
    }
	function redirectToPage($page)
	{
		header("Location: index.php?page=$page");
	}

	function internalLink ($text, $page, $roles, $args="", $failReturnText = "")
	{
		if (is_array($args))
		{
			$argString = "";
			foreach ($args as $key => $value)
			{
				$argString .= "&$key=$value";
			}
			$args = $argString;
		}

		$html = $failReturnText;
		if (UserUtil::hasPermission($roles, $page))
		{
			$html = "<a href=\"?page=$page" . $args . "\">$text</a>";
		}

		return $html;
	}

	function internalAsynchLink ($text, $page, $roles, $div="", $args="")
	{
		if ($div == "")
		{
			$div = $page . "Div";
		}
		if (is_array($args))
		{
			$argString = "";
			foreach ($args as $key => $value)
			{
				$argString .= "&$key=$value";
			}
			$args = substr($argString, 1);
		}

		$html = "";
		if (UserUtil::hasPermission($roles, $page))
		{
			$html = "<a href=\"javascript:asynchAction('$page', '$div', '$args');\">$text</a>";
		}

		return $html;
	}

	function tableDropdown($name, $selectedValue, $optionObjects, $label, $value="id")
	{
		$html = "";
		$html .= "<select name=\"$name\">\n";
		$html .= "\t<option value=\"\">---</option>\n";
	    foreach ($optionObjects as $object)
	    {
	    	$finalLabel = $label;
	    	$words = split("[ ,\(\)\\]+", trim($label));
	    	foreach ($words as $word)
	    	{
	    		$labelValue = $object->$word;
	    		$finalLabel = str_replace($word, $labelValue, $finalLabel);
	    	}
	    	$html .=  "\t<option value=\"" . $object->$value . "\"";
	    	if ($selectedValue == $object->$value) $html .=  " selected";
	    	$html .=  ">$finalLabel</option>\n";
	    }
	    $html .=  "</select>";

	    return $html;
	}

	function dropdown($name, $selectedValue=null, $table=null)
	{
		if ($table == null)
		{
			$codeLookupOptions = CodeLookupUtil::getCodes($name);
		}
		else
		{
			$codeLookupOptions = CodeLookupUtil::getCodes($table);
		}
		$html = "";
		$html .= "<select name=\"$name\">\n";
		$html .= "\t<option value=\"\">---</option>\n";
	    foreach ($codeLookupOptions as $codeLookup)
	    {
	    	$html .=  "\t<option value=\"$codeLookup->code_key\"";
	    	if ($selectedValue == $codeLookup->code_key) $html .=  " selected";
	    	$html .=  ">$codeLookup->value</option>\n";
	    }
	    $html .=  "</select>";

	    return $html;
	}



}

?>
